package service.Impl;


import com.alibaba.fastjson.JSON;
import core.ParseRequestData;
import mapper.Impl.TeacherMapperImpl;
import mapper.TeacherMapper;
import org.apache.commons.io.IOUtils;
import pojo.Teacher;
import service.CourseService;
import service.TeacherService;
import utils.CookieUtils;
import utils.ParseRequestDataUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TeacherServiceImpl implements TeacherService {

    private TeacherMapper teacherMapper = new TeacherMapperImpl();
    private CourseService courseService = new CourseServiceImpl();

    @Override
    public boolean isAdd(String username) {
        return false;
    }

    @Override
    public boolean add(HttpServletRequest request, HttpServletResponse response) {
        return false;
    }

    @Override
    public String initModify(HttpServletRequest request, HttpServletResponse response) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("user");
        return JSON.toJSONString(teacher);
    }

    @Override
    public String modify(HttpServletRequest request, HttpServletResponse response) {

        ParseRequestData<Teacher> parseRequestData = ParseRequestDataUtils.parseRequest(request, Teacher.class, "upload\\teacher");
        Teacher tea = parseRequestData.getT();
        InputStream in = parseRequestData.getIn();
        OutputStream out = parseRequestData.getOut();

        String code="0";
        try {
            if(tea.getPhoto() != null){//说明用户修改头像
                teacherMapper.update(tea.getUsername(),tea.getName(), tea.getSex(), tea.getAge(),tea.getCourseId(),tea.getPhoto());
                IOUtils.copy(in,out);
            }else{
                teacherMapper.update(tea.getUsername(),tea.getName(), tea.getSex(), tea.getAge(),tea.getCourseId());
            }
            if(in != null){
                in.close();
            }
            if(out != null){
                out.close();
            }

            code="1";
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        String role = (String) request.getSession().getAttribute("role");
        if("teacher".equals(role)){
            //更新Session里的数据
            request.getSession().setAttribute("name",tea.getName());
            //更新Cookie里的数据
            response.addCookie(CookieUtils.createCookie("name",tea.getName(),60*60*24*5));

            if (tea.getPhoto() != null){
                request.getSession().setAttribute("photo",tea.getPhoto());
                response.addCookie(CookieUtils.createCookie("photo",tea.getPhoto(),60*60*24*5));
            }
        }
        return code;
    }

    @Override
    public void getTeachers(HttpServletRequest request, HttpServletResponse response) {

    }

    @Override
    public void delete(HttpServletRequest request, HttpServletResponse response) {

    }
}
